<template>
  <div>
    <el-card class="box-card margin">
      <div slot="header" class="flex justify-between">
        <span>{{ $t('hireParkingSpace.title') }}</span>
        <div class="">
          <el-button type="primary" size="small" icon="el-icon-close" @click="goBack">
            {{ $t('common.back') }}
          </el-button>
        </div>
      </div>

      <el-form ref="form" :model="hireParkingSpaceInfo" label-width="120px" label-position="right">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('hireParkingSpace.carNum')" prop="carNum" required>
              <el-input v-model="hireParkingSpaceInfo.carNum" :placeholder="$t('hireParkingSpace.carNumPlaceholder')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('hireParkingSpace.carBrand')">
              <el-input v-model="hireParkingSpaceInfo.carBrand"
                :placeholder="$t('hireParkingSpace.carBrandPlaceholder')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('hireParkingSpace.carType')" prop="carType" required>
              <el-select v-model="hireParkingSpaceInfo.carType" class="w-full"
                :placeholder="$t('hireParkingSpace.carTypePlaceholder')">
                <el-option v-for="carType in hireParkingSpaceInfo.carTypes" :key="carType.statusCd" :label="carType.name"
                  :value="carType.statusCd" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('hireParkingSpace.carColor')">
              <el-input v-model="hireParkingSpaceInfo.carColor"
                :placeholder="$t('hireParkingSpace.carColorPlaceholder')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('hireParkingSpace.licenseType')" prop="leaseType" required>
              <el-select v-model="hireParkingSpaceInfo.leaseType" class="w-full"
                :placeholder="$t('hireParkingSpace.licenseTypePlaceholder')" @change="changeLeaseType">
                <el-option :label="$t('hireParkingSpace.monthlyRent')" value="H" />
                <el-option :label="$t('hireParkingSpace.saleCar')" value="S" />
                <el-option :label="$t('hireParkingSpace.internalCar')" value="I" />
                <el-option :label="$t('hireParkingSpace.freeCar')" value="NM" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row v-if="showDateRange" :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('hireParkingSpace.startTime')" prop="startTime" required>
              <el-date-picker v-model="hireParkingSpaceInfo.startTime" type="date" value-format="yyyy-MM-dd" class="w-full"
                :placeholder="$t('hireParkingSpace.startTimePlaceholder')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('hireParkingSpace.endTime')" prop="endTime" required>
              <el-date-picker v-model="hireParkingSpaceInfo.endTime" type="date" value-format="yyyy-MM-dd" class="w-full"
                :placeholder="$t('hireParkingSpace.endTimePlaceholder')" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item :label="$t('hireParkingSpace.owner')" prop="ownerName" required>
              <el-input v-model="hireParkingSpaceInfo.ownerName" disabled
                :placeholder="$t('hireParkingSpace.ownerPlaceholder')" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
              <div class="">
                <el-button type="primary" size="small" @click="openChooseOwner">
                  {{ $t('common.select') }}
                </el-button>
              </div>
          </el-col>
          <el-col :span="8">
            <el-form-item :label="$t('hireParkingSpace.parkingSpace')" prop="psName" required>
              <el-input v-model="hireParkingSpaceInfo.psName" disabled
                :placeholder="$t('hireParkingSpace.parkingSpacePlaceholder')" />
            </el-form-item>
          </el-col>
          <el-col :span="4">
                <el-button type="primary" size="small" @click="openSearchParkingSpaceModel">
                  {{ $t('common.select') }}
                </el-button>
          </el-col>
        </el-row>

        <el-row v-for="(item, index) in hireParkingSpaceInfo.attrs" :key="index">
          <el-col :span="24">
            <el-form-item :label="item.specName">
              <el-input v-if="item.specType === '2233'" v-model="item.value" :placeholder="item.specHoldplace" />
              <el-select v-else-if="item.specType === '3344'" v-model="item.value" class="w-full"
                :placeholder="item.specHoldplace">
                <el-option v-for="(val, idx) in item.values" :key="idx" :label="val.valueName" :value="val.value" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="24">
            <el-form-item :label="$t('hireParkingSpace.remark')">
              <el-input v-model="hireParkingSpaceInfo.remark" type="textarea" :rows="3"
                :placeholder="$t('hireParkingSpace.remarkPlaceholder')" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </el-card>

    <div class="mt-20 text-right">
      <el-button @click="goBack">{{ $t('common.back') }}</el-button>
      <el-button type="primary" @click="saveAddCarInfo">
        {{ $t('common.submit') }}
      </el-button>
    </div>

    <search-owner ref="searchOwner" @chooseOwner="handleChooseOwner" />
    <search-parking-space ref="searchParkingSpace" @choose-parking-space="handleChooseParkingSpace" />
  </div>
</template>

<script>
import { saveOwnerCar } from '@/api/car/hireParkingSpaceApi'
import { getCommunityId, getDict } from '@/api/community/communityApi'
import SearchOwner from '@/components/owner/SearchOwner'
import SearchParkingSpace from '@/components/car/SearchParkingSpace'
import { getSpecAttrList } from '@/api/dev/attrSpecApi'

export default {
  name: 'HireParkingSpaceList',
  components: {
    SearchOwner,
    SearchParkingSpace
  },
  data() {
    return {
      hireParkingSpaceInfo: {
        carNum: '',
        carBrand: '',
        carType: '',
        carColor: '',
        remark: '',
        startTime: '',
        endTime: '',
        leaseType: 'H',
        ownerId: '',
        ownerName: '',
        psId: '',
        psName: '',
        attrs: [],
        carTypes: []
      }
    }
  },
  computed: {
    showDateRange() {
      return ['H', 'S'].includes(this.hireParkingSpaceInfo.leaseType)
    }
  },
  async created() {
    await this.initData()
    const { ownerId, ownerName } = this.$route.query
    if (ownerId) {
      this.hireParkingSpaceInfo.ownerId = ownerId
      this.hireParkingSpaceInfo.ownerName = ownerName
    }
  },
  methods: {
    async initData() {
      this.communityId = getCommunityId()

      // 加载车辆类型字典
      try {
        const carTypes = await getDict('owner_car', 'car_type')
        this.hireParkingSpaceInfo.carTypes = carTypes
      } catch (error) {
        console.error('加载车辆类型失败:', error)
      }

      // 加载车辆属性规格
      try {
        await this.loadCarAttrSpec()
      } catch (error) {
        console.error('加载车辆属性失败:', error)
      }
    },

    async loadCarAttrSpec() {
      try {
        const attrs = await getSpecAttrList('owner_car_attr')
        this.hireParkingSpaceInfo.attrs = attrs.map(item => {
          if (item.specShow === 'Y') {
            return {
              ...item,
              value: '',
              values: item.specType === '3344' ? [] : undefined
            }
          }
          return null
        }).filter(Boolean)
      } catch (error) {
        console.error('加载车辆属性规格失败:', error)
      }
    },

    changeLeaseType() {
      this.hireParkingSpaceInfo.startTime = ''
      this.hireParkingSpaceInfo.endTime = ''
    },

    openChooseOwner() {
      this.$refs.searchOwner.open()
    },

    handleChooseOwner(owner) {
      this.hireParkingSpaceInfo.ownerName = owner.name
      this.hireParkingSpaceInfo.ownerId = owner.memberId
    },

    openSearchParkingSpaceModel() {
      this.$refs.searchParkingSpace.open()
    },

    handleChooseParkingSpace(parkingSpace) {
      this.hireParkingSpaceInfo.psName = `${parkingSpace.areaNum}-${parkingSpace.num}`
      this.hireParkingSpaceInfo.psId = parkingSpace.psId
    },

    validateForm() {
      const requiredFields = [
        { field: 'carNum', message: this.$t('hireParkingSpace.validate.carNum') },
        { field: 'carType', message: this.$t('hireParkingSpace.validate.carType') },
        { field: 'leaseType', message: this.$t('hireParkingSpace.validate.licenseType') },
        { field: 'ownerId', message: this.$t('hireParkingSpace.validate.owner') },
        { field: 'psId', message: this.$t('hireParkingSpace.validate.parkingSpace') }
      ]

      for (const { field, message } of requiredFields) {
        if (!this.hireParkingSpaceInfo[field]) {
          this.$message.error(message)
          return false
        }
      }

      // 验证日期范围
      if (this.showDateRange) {
        if (!this.hireParkingSpaceInfo.startTime || !this.hireParkingSpaceInfo.endTime) {
          this.$message.error(this.$t('hireParkingSpace.validate.dateRange'))
          return false
        }
      }

      // 验证自定义属性
      for (const attr of this.hireParkingSpaceInfo.attrs) {
        if (attr.required === 'Y' && !attr.value) {
          this.$message.error(attr.specHoldplace)
          return false
        }
      }

      return true
    },

    async saveAddCarInfo() {
      if (!this.validateForm()) return

      try {
        const params = {
          ...this.hireParkingSpaceInfo,
          communityId: this.communityId
        }

        await saveOwnerCar(params)
        this.$message.success(this.$t('hireParkingSpace.saveSuccess'))
        this.goBack()
      } catch (error) {
        console.error('保存失败:', error)
        this.$message.error(this.$t('hireParkingSpace.saveError'))
      }
    },

    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>

<style scoped>
.card-header-actions {
  position: absolute;
  right: 20px;
  top: 15px;
}

.w-full {
  width: 100%;
}

.mt-10 {
  margin-top: 10px;
}

.mt-20 {
  margin-top: 20px;
}
</style>